//
//  WBBaseViewController.swift
//  微博
//
//  Created by WangQiang on 2017/8/29.
//  Copyright © 2017年 WangQiang. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {
    let navItem = UINavigationItem.init()
    
    
    var userLogin = false
    
    var tableView:UITableView?
    var refreshControl:UIRefreshControl?
    //上拉刷新
    var isPullup  = true
    
    var visitorInfoDictionary:[String:String]?
    
    
  fileprivate lazy var navigationBar = UINavigationBar.init(frame: CGRect.init(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: 64.0))
    
  
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    override var title: String?{
        didSet{
            navItem.title = title
        }
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
}
//MARK: =========== 设置界面 ===========
extension WBBaseViewController{
    fileprivate func setupUI(){
        // 取消自动缩进 - 如果隐藏了导航栏，会缩进 20 个点
        automaticallyAdjustsScrollViewInsets = false
        setupNavagationBar()
        
        userLogin ? setupTableView() : setupVistorView()
    }
    
    private func setupTableView(){
        tableView = UITableView.init()
        tableView?.delegate = self
        tableView?.dataSource = self
        view.insertSubview(tableView!, belowSubview: navigationBar)
        tableView?.snp.makeConstraints({ (make) in
            make.edges.equalTo(self.view)
        })
        tableView?.contentInset = UIEdgeInsetsMake(64.0, 0, 0, 0)
        refreshControl = UIRefreshControl.init()
//        #selector(refreshData)
        refreshControl?.addTarget(self, action: #selector(refreshData(sender:)), for: .valueChanged)
        
        if #available(iOS 10.0, *) {
            tableView?.refreshControl = refreshControl!
        } else {
            // Fallback on earlier versions
        }
        
    }
    private func setupVistorView(){
        let  vistorView = WBVistorView.init(frame: .zero)
        view.insertSubview(vistorView, belowSubview: navigationBar)
        vistorView.visitorInfo = visitorInfoDictionary
        vistorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        vistorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
//        vistorView.backgroundColor = UIColor.red
        vistorView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(self.view.snp.top).offset(64.0)
        }
        
    }
    
    private func setupNavagationBar(){
        view.addSubview(navigationBar)
        navigationBar.barTintColor = UIColor.init(hex: 0xF6F6F6)
        navigationBar.items = [navItem]
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        navigationBar.tintColor = UIColor.orange
    }
}
//MARK: =========== tableView 刷新跟加载更多 ===========
extension WBBaseViewController{
    @objc fileprivate func refreshData(sender:UIRefreshControl){
        
        sender.endRefreshing()
        
        print("刷新数据")
    }
    fileprivate func loadData(){
        print("加载更多数据")
    }
}
//MARK: =========== tableView delegate ,dataSource  ===========
extension WBBaseViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //此处只是保证语法无错
        return UITableViewCell()
    }
}
//MARK: =========== 访客视图监听方法 ===========
extension WBBaseViewController{
   @objc fileprivate func login()  {
        print("登录")
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBUserShouldLoginNotification), object: nil)
    }
    @objc fileprivate func register(){
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBUserShouldLoginNotification), object: nil)
        
        print("注册")
    }
}
